package org.example.travel.config;

import lombok.RequiredArgsConstructor;
import org.example.travel.interceptor.LoginInterceptor;
import org.example.travel.interceptor.TokenInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@RequiredArgsConstructor
public class WebConfig implements WebMvcConfigurer {

    private final StringRedisTemplate redisTemplate;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 第一层：TokenInterceptor 解析用户身份，刷新 token
        registry.addInterceptor(new TokenInterceptor(redisTemplate))
                .addPathPatterns("/**")
                .excludePathPatterns(
                        "/user/send-code",
                        "/user/register",
                        "/user/login-by-password",
                        "/scenic/log"
                );

        // 第二层：LoginInterceptor 拦截必须登录的接口
        registry.addInterceptor(new LoginInterceptor())
                .addPathPatterns(
                        "/user/me",
                        "/order/**",
                        "/favorite/**"
                );
    }
}
